/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package unique;

//import backup.*;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniel
 */
public class ServerThread extends Thread {

    Socket reqSocket;
    WebServer servidor;
    BufferedReader doCliente;
    PrintStream paraCliente;
    String requisicao;

    public ServerThread(Socket s, WebServer serv) {
        this.reqSocket = s;
        this.servidor = serv;

        try {
            paraCliente = new PrintStream(s.getOutputStream());
            doCliente = new BufferedReader(new InputStreamReader(reqSocket.getInputStream()));
            requisicao = doCliente.readLine();
        } catch (IOException e) {
            System.out.println("Erro ao tentar abrir os stream de conexão: " + e);
        }
        System.out.println("passou do construtor");
    }

    public void answerIndex() {
        System.out.println("1");
        servidor.getCardsList().resetNextCard();
        System.out.println("2");
        String temp;
        try {
            while ((temp = servidor.getCardsList().getNextCard().toString()) != null) {
                paraCliente.println(temp);
                System.out.println(temp);
            }
            paraCliente.println((String) null);
        } catch (Exception e) {
            System.out.println("olha ai");
        }
    }

    public void answerRemove (String token) {
        PrintStream ps = null;
        try {
            ps = new PrintStream(reqSocket.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        int id = Integer.parseInt(token.split(" :: ")[1]);
        servidor.remove(id);
        
        ps.println ((String) null);
    }
    
    public void answerAdd (String token) {
        PrintStream ps = null;
        try {
            ps = new PrintStream(reqSocket.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        servidor.add (token);
        
        ps.println((String) null);
    }

    @Override
    public void run() {
        System.out.println("entrou no run");
        synchronized (this) {

            try {
                if (requisicao.split(" :: ")[0].equals("REMOVE")) {
                    System.out.println ("[Server] answering to remove.");
                    answerRemove(requisicao);
                } else if (requisicao.split(" :: ")[0].equals("ADD")) {
                    System.out.println ("[Server] answering to add.");
                    answerAdd(requisicao);
                } else if (requisicao.split(" :: ")[0].equals("EDIT")) {
                    System.out.println ("[Server] answering to edit.");
 //                   servidor.answerEdit(requisicao);
                } else {
                    System.out.println ("[Server] answering to index.");
                    answerIndex();
                }
                reqSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
